package cn.ssms.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.ssms.alipay.config.AlipayConfig;
import cn.ssms.alipay.util.OrderInfoUtil2_0;
import cn.ssms.dao.CardMapper;
import cn.ssms.dao.CardRecordMapper;
import cn.ssms.dao.ConstantMapper;
import cn.ssms.dao.CourseMapper;
import cn.ssms.dao.CourseUserMapper;
import cn.ssms.dao.StoreMapper;
import cn.ssms.dao.TelCodeMapper;
import cn.ssms.dao.UserMapper;
import cn.ssms.model.Card;
import cn.ssms.model.CardRecord;
import cn.ssms.model.Constant;
import cn.ssms.model.Course;
import cn.ssms.model.CourseUser;
import cn.ssms.model.Store;
import cn.ssms.model.TelCode;
import cn.ssms.model.TokenInfo;
import cn.ssms.model.User;
import cn.ssms.util.DataAccessException;
import cn.ssms.util.DateUtil;
import cn.ssms.util.MD5Util;
import cn.ssms.util.MyUtils;
import cn.ssms.util.PrimaryKeyGenerate;
import cn.ssms.util.Sendsms;

@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private ConstantMapper constantMapper;
	@Autowired
	private TelCodeMapper telCodeMapper;
	@Autowired
	private CardMapper cardMapper;
	@Autowired
	private StoreMapper storeMapper;
	@Autowired
	private CourseMapper courseMapper;
	@Autowired
	private CardRecordMapper cardRecordMapper;
	@Autowired
	private CourseUserMapper courseUserMapper;
	

	@Transactional(propagation=Propagation.REQUIRED,rollbackFor = Exception.class) 
	@Override
	public Map<String, Object> getToken(Map<String, Object> map) throws DataAccessException  {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tel = map.get("tel")+"";
		String password = map.get("password")+"";		
		//验证传入值的正确性
		boolean flag = true;
		//tel
		User user = new User();
		if(MyUtils.isMobile(tel)){			
			//System.out.println(MD5Util.MD5(password));
			user = userMapper.queryUserByTel(tel);
			if(user == null||(!user.getPassword().equals(MD5Util.MD5(password)))) {
				flag = false;
			}
		}else{
			flag = false;
		}
		
		if(flag) {
			//用于异常操作
			String token="";			
			//token有效时间断
			Constant tokenlife= constantMapper.queryConstantByName("TOKENLIFE");
			//User user=userMapper.queryUserByTel(tel);
			token=user.getTokenid();
			//token还没设置，或则token过期，重新设置	
			User  userMes =  new User();
			if(token==null||"".equals(token)||DateUtil.String2Date(user.getTokenovertime()).before(new Date())){
				token=PrimaryKeyGenerate.getInstance("TOK");					
				userMes.setUserid(user.getUserid());
				userMes.setTokenid(token);					
				userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
				userMapper.updateUserMes(userMes);
			}
			result.put("code", 200);
			result.put("message", "获取TOKEN成功");				
			TokenInfo tokenInfo=new TokenInfo();
			tokenInfo.setLife(tokenlife.getValue());
			tokenInfo.setTokenid(token);
			result.put("data", tokenInfo);				
		
		}else {
			throw new DataAccessException("获取TOKEN失败， 账号信息错误");				
		}		
		return result;
	}

	@Transactional(propagation=Propagation.REQUIRED,rollbackFor = Exception.class) 
	@Override
	public Map<String, Object> regist(Map<String, Object> map) throws DataAccessException  {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tel = map.get("tel")+"";
		String password = map.get("password")+"";	
		String telcode = map.get("telcode")+"";	
		//验证传入值的正确性	
		boolean flag = true;
		String  mes = "";
		if(map.get("telcode")==null||"".equals(telcode)){
			flag = false;
			mes="验证码不能为空";
		}
		if(map.get("password")==null||"".equals(password)){
			flag = false;
			mes="密码不能为空";
		}
		if((map.get("tel")==null||!MyUtils.isMobile(tel))){
			flag = false;
			mes="手机号不符合规则";
		}
		
		if(flag) {
			User user = userMapper.queryUserByTel(tel);
			TelCode telCode=telCodeMapper.queryByTel(tel);
			//电话号码已存在
			if(user!=null){
				result.put("code", 250);
				result.put("message", "该手机号已注册");				
			}else{
				//判断验证码是否正确
				if(telCode==null||telCode.getTelcode()==null||!telCode.getTelcode().equals(telcode)){
					result.put("code", 260);
					result.put("message", "验证码错误");
				}else if(telCode.getTelcode().equals(telcode)&&(DateUtil.AfterSSDate(-1*Integer.parseInt(constantMapper.queryConstantByName("TELCODELIFE").getValue())).after(DateUtil.String2Date(telCode.getAdddate())))){
					result.put("code", 270);
					result.put("message", "验证码超时");
				}else{
					String id=PrimaryKeyGenerate.getInstance("USE");
					//用户账号信息					
					User usermes=new User();					
					usermes.setUserid(id);
					usermes.setGender("2");
					usermes.setName(tel);
					usermes.setCardname("非会员");  //初始化会员类型
					usermes.setPoints(0); //初始化积分
					usermes.setIcon(constantMapper.queryConstantByName("ICONPATH").getValue());//默认头像
					usermes.setSignature(constantMapper.queryConstantByName("SIGNATURE").getValue());//默认签名
					//会员卡过期时间(默认时间)
					usermes.setCardovertime("2000-01-01 00:00:00");
					//最近一次违约时间(默认时间)
					usermes.setDefoulttime("2000-01-01 00:00:00");
					//token
					usermes.setTokenid(PrimaryKeyGenerate.getInstance("TOK"));
					//token过期时间
					usermes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					usermes.setVersion(0);
					userMapper.insertUserMes(usermes);//插入用户信息表					
					//插入账号
					User newuser=new User();
					newuser.setUserid(id);
					newuser.setTel(tel);
					newuser.setPassword(MD5Util.MD5(password)); 
					newuser.setVersion(0);
					userMapper.insertUser(newuser);//插入用户表	
					User userr = userMapper.queryUserById(id);
					Constant constant = constantMapper.queryConstantByName("ICONURL");	
					userr.setIcon(constantMapper.queryConstantByName("URL_FRONT").getValue()+constant.getValue()+userr.getIcon());
					result.put("code", 200);
					result.put("message", "注册成功");		
					result.put("data", userr);		
				}
			}		
		
		}else {
			throw new DataAccessException(mes);				
		}		
		return result;
	}

	@Override
	public Map<String, Object> telIsRegisted(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tel = map.get("tel")+"";		
		//验证传入值的正确性
		boolean flag = true;
		//tel		
		if(!MyUtils.isMobile(tel)){
			flag = false;
		}		
		if(flag) {
			User user = userMapper.queryUserByTel(tel);
			//电话号码已存在
			if(user==null){
				result.put("code", 200);
				result.put("message","该手机号未注册");				
			}else{
				result.put("code",250);
				result.put("message","该手机号已注册");				
			}	
		
		}else {
			throw new DataAccessException("手机号不符合规则");				
		}		
		return result;
	}

	@Transactional(propagation=Propagation.REQUIRED,rollbackFor = Exception.class) 
	@Override
	public Map<String, Object> sendsms(Map<String, Object> map)throws DataAccessException  {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tel = map.get("tel")+"";
		String type = map.get("type")+"";		
		if(MyUtils.isMobile(tel)){
			if(null!=map.get("type")&&!"".equals(type)){
				if("0".equals(type)) { //0 注册
					
						User user = userMapper.queryUserByTel(tel);
						//电话号码是否存在
						if(user==null){						
							String mobile_code = (int)((Math.random()*9+1)*100000)+"";							
							result = Sendsms.getCode(tel, mobile_code);
							TelCode telCode = new TelCode();
							telCode.setId(PrimaryKeyGenerate.getInstance("TEL"));
							telCode.setTelcode(mobile_code);
							telCode.setTel(tel);
							telCode.setType(type);	
							telCode.setVersion(0);
							telCodeMapper.deleteTelCode(tel);
							telCodeMapper.insertTelCode(telCode);
							
						}else{
							result.put("code",1);
							result.put("message","该手机号已注册");							
						}
					
				}else if("1".equals(type)){ //1 忘记密码
					
						User user = userMapper.queryUserByTel(tel);
						//电话号码是否存在
						if(user==null){
							result.put("code",1);
							result.put("message","该用户不存在");							
						}else{
							String mobile_code = (int)((Math.random()*9+1)*100000)+"";
							result = Sendsms.getCode(tel, mobile_code);
							TelCode telCode = new TelCode();
							telCode.setId(PrimaryKeyGenerate.getInstance("TEl"));
							telCode.setTelcode(mobile_code);
							telCode.setTel(tel);
							telCode.setType(type);
							telCode.setVersion(0);
							telCodeMapper.deleteTelCode(tel);
							telCodeMapper.insertTelCode(telCode);
						}
					
				}
			}else{				
				throw new DataAccessException("请求参数类型错误");			
			}
		}else{
			throw new DataAccessException("手机号不符合规则");
		}
		return result;
	}

	@Override
	public Map<String, Object> getUserMessage(Map<String, Object> map)throws DataAccessException  {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tokenid = map.get("tokenid")+"";
		String userid = map.get("userid")+"";		
		//验证传入值的正确性
		boolean flag = true;
		User usermes = new User();
		String mes = "";
		if(map.get("tokenid")==null||"".equals(tokenid)){			
			flag = false;
			mes="token不为空，验证失败";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}
			}		
		}
		if(flag) {
			if(map.get("userid")==null||"".equals(userid)){			
				userid=usermes.getUserid();
			}
			User user = userMapper.queryUserById(userid);
			if(user!=null){
					result.put("code",200);					
					result.put("message", "获取用户信息成功");				
					Constant constant = constantMapper.queryConstantByName("ICONURL");	
					user.setIcon(constantMapper.queryConstantByName("URL_FRONT").getValue()+constant.getValue()+user.getIcon());
					result.put("data",user);

			}else{
				throw new DataAccessException("获取用户信息失败,用户为空");					
			}		
		}else {
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> updatePassword(Map<String, Object> map)throws DataAccessException  {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tokenid = map.get("tokenid")+"";
		String password = map.get("password")+"";		
		//验证传入值的正确性
		boolean flag = true;
		User usermes = new User();
		String mes = "";
		if(map.get("tokenid")==null||"".equals(tokenid)){			
			flag = false;
			mes="token不为空，验证失败";
		}else if(map.get("password")==null||"".equals(password)){			
			flag = false;
			mes="password不为空，验证失败";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}
			}		
		}
		if(flag) {
			User user = new User();
			user.setUserid(usermes.getUserid());
			user.setPassword(MD5Util.MD5(password));
			userMapper.updateUser(user);
			result.put("code", 200);
			result.put("message","修改密码成功");
		}else {
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> updateName(Map<String, Object> map)throws DataAccessException  {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tokenid = map.get("tokenid")+"";
		String name = map.get("name")+"";		
		//验证传入值的正确性
		boolean flag = true;
		User usermes = new User();
		String mes = "";
		if(map.get("tokenid")==null||"".equals(tokenid)){			
			flag = false;
			mes="token不为空，验证失败";
		}else if(map.get("name")==null||"".equals(name)){			
			flag = false;
			mes="name不为空，验证失败";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}
			}		
		}
		if(flag) {
			User user = new User();
			user.setUserid(usermes.getUserid());
			user.setName(name);
			userMapper.updateUserMes(user);
			result.put("code", 200);
			result.put("message","修改名字成功");
		}else {
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> updateSign(Map<String, Object> map)throws DataAccessException  {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tokenid = map.get("tokenid")+"";
		String signature = map.get("signature")+"";		
		//验证传入值的正确性
		boolean flag = true;
		User usermes = new User();
		String mes = "";
		if(map.get("tokenid")==null||"".equals(tokenid)){			
			flag = false;
			mes="token不为空，验证失败";
		}else if(map.get("signature")==null||"".equals(signature)){			
			flag = false;
			mes="signature不为空，验证失败";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}
			}		
		}
		if(flag) {
			User user = new User();
			user.setUserid(usermes.getUserid());
			user.setSignature(signature);
			userMapper.updateUserMes(user);
			result.put("code", 200);
			result.put("message","修改个性签名成功");
		}else {
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> updateSex(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tokenid = map.get("tokenid")+"";
		String sex = map.get("sex")+"";		
		//验证传入值的正确性
		boolean flag = true;
		User usermes = new User();
		String mes = "";
		if(map.get("tokenid")==null||"".equals(tokenid)){			
			flag = false;
			mes="token不为空，验证失败";
		}else if(map.get("sex")==null||"".equals(sex)){			
			flag = false;
			mes="sex不为空，验证失败";
		}else if(!"0".equals(sex)&&!"1".equals(sex)){
			flag = false;
			mes="sex数据错误，验证失败";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}
			}		
		}
		if(flag) {
			User user = new User();
			user.setUserid(usermes.getUserid());
			user.setGender(sex);
			userMapper.updateUserMes(user);
			result.put("code", 200);
			result.put("message","修改性别成功");
		}else {
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> updateIcon(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tokenid = map.get("tokenid")+"";
		String icon = map.get("icon")+"";		
		//验证传入值的正确性
		boolean flag = true;
		User usermes = new User();
		String mes = "";
		if(map.get("tokenid")==null||"".equals(tokenid)){			
			flag = false;
			mes="token不为空，验证失败";
		}else if(map.get("icon")==null||"".equals(icon)){			
			flag = false;
			mes="icon不为空，验证失败";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}
			}		
		}
		if(flag) {
			Constant constant = constantMapper.queryConstantByName("ICONURLNAME");	
			String path = map.get("path") +constant.getValue();
			String fileName = MyUtils.saveIcon(icon, path, usermes.getUserid());
			User user = new User();
			user.setUserid(usermes.getUserid());
			user.setIcon(fileName);
			userMapper.updateUserMes(user);
			result.put("code", 200);
			result.put("message","上传头像成功");
		}else {
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> getCards(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();
		List<Card> cards =	cardMapper.getCards();
		result.put("code", 200);
		result.put("message","获取会员卡信息成功");
		result.put("data", cards);
		return result;
	}

	@Override
	public Map<String, Object> applogin(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tel = map.get("tel")+"";
		String password = map.get("password")+"";		
		//验证传入值的正确性
		boolean flag = true;
		String mes="";
		if(map.get("password")==null||"".equals(password)){
			flag = false;
			mes="密码不能为空";
		}
		//tel
		if(map.get("tel")==null||"".equals(tel)){
			flag = false;
			mes="手机号不能为空";
		}		
		User user =  new User();
		if(MyUtils.isMobile(tel)){
			user = userMapper.queryUserByTel(tel);
			password = MD5Util.MD5(password);
			if(user == null||(!user.getPassword().equals(password))) {
				flag = false;
				mes="账号或密码错误";
			}
		}else{
			flag = false;
			mes="手机号不符合规则";
		}
		if(flag) {
			String token=user.getTokenid();
			//token还没设置，或则token过期，重新设置					
			if(token==null||"".equals(token)||DateUtil.String2Date(user.getTokenovertime()).before(new Date())){
				User userMes =  new User();
				token=PrimaryKeyGenerate.getInstance("TOK");					
				userMes.setUserid(user.getUserid());
				userMes.setTokenid(token);					
				userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
				userMapper.updateUserMes(userMes);
			}
			User userr = userMapper.queryUserById(user.getUserid());
			Constant constant = constantMapper.queryConstantByName("ICONURL");	
			userr.setIcon(constantMapper.queryConstantByName("URL_FRONT").getValue()+constant.getValue()+userr.getIcon());
			result.put("code", 200);
			result.put("message", "登录成功");		
			result.put("data", userr);				
		}else {
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> storeComboBox(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();
		List<Store> stores = storeMapper.getStores();
		result.put("code", 200);
		result.put("message","获取门店下拉框成功");
		result.put("data", stores);
		return result;
	}

	@Override
	public Map<String, Object> forgetPassword(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();
		String tel = map.get("tel")+"";
		String password = map.get("password")+"";	
		String telcode = map.get("telcode")+"";	
		//验证传入值的正确性	
		boolean flag = true;
		String  mes = "";
		if(map.get("telcode")==null||"".equals(telcode)){
			flag = false;
			mes="验证码不能为空";
		}
		if(map.get("password")==null||"".equals(password)){
			flag = false;
			mes="密码不能为空";
		}
		if((map.get("tel")==null||!MyUtils.isMobile(tel))){
			flag = false;
			mes="手机号不符合规则";
		}
		
		if(flag) {
			User user = userMapper.queryUserByTel(tel);
			TelCode telCode=telCodeMapper.queryByTel(tel);
			//电话号码不存在
			if(user==null){
				result.put("code", 250);
				result.put("message", "该手机号未注册");				
			}else{
				//判断验证码是否正确
				if(telCode==null||telCode.getTelcode()==null||!telCode.getTelcode().equals(telcode)){
					result.put("code", 260);
					result.put("message", "验证码错误");
				}else if(telCode.getTelcode().equals(telcode)&&(DateUtil.AfterSSDate(-1*Integer.parseInt(constantMapper.queryConstantByName("TELCODELIFE").getValue())).after(DateUtil.String2Date(telCode.getAdddate())))){
					result.put("code", 270);
					result.put("message", "验证码超时");
				}else{				
					//用户账号信息							
					User userUp=new User();
					userUp.setUserid(user.getUserid());				
					userUp.setPassword(MD5Util.MD5(password)); 				
					userMapper.updateUser(userUp);		
					result.put("code", 200);
					result.put("message", "找回密码成功");							
				}
			}		
		
		}else {
			throw new DataAccessException(mes);				
		}		
		return result;
	}

	@Override
	public Map<String, Object> getListCourse(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();	
		String tokenid = map.get("tokenid")+"";			
		String userid="";
		//先获取userID
		if(map.get("tokenid")!=null&&!"".equals(tokenid)){
			User userMes=userMapper.queryUserByToken(tokenid);
			if(userMes!=null){			
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(userMes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User user = new User();
					user.setUserid(userMes.getUserid());
					user.setTokenid(tokenid);					
					user.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(user);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}else{
					userid=userMes.getUserid();		
				}			
				
			}		
		}
		map.put("userid", userid);
		Integer total = courseMapper.getListCourseCount(map);
		List<Course> courses = courseMapper.getListCourse(map);
		Map<String, Object> data =  new HashMap<String, Object>();
		data.put("totalCount", total);
		data.put("list", courses);
		Constant constant = constantMapper.queryConstantByName("IMAGE_URLHEAD");	
		data.put("imgUrl", constant.getValue());
		result.put("data", data);
		result.put("code", 200);
		result.put("message", "获取课程信息成功");			
		return result;
	}

	@Transactional(propagation=Propagation.REQUIRED,rollbackFor = Exception.class)
	@Override
	public Map<String, Object> buyCard(Map<String, Object> map)	throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();	
		String tel = map.get("tel")+"";
		String tokenid = map.get("tokenid")+"";	
		String cardid = map.get("cardid")+"";	
		boolean flag = true;	
		String mes = "";				
		User usermes = new User();
		if(map.get("tokenid")==null||"".equals(tokenid)){
			flag = false;
			mes="token不能为空";
		}else if(map.get("cardid")==null||"".equals(cardid)){
			flag = false;
			mes="会员卡不能为空";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}else{
					User myuser = userMapper.queryUserById(usermes.getUserid());
					if(map.get("tel")!=null&&tel.equals(myuser.getTel())){
						flag = false;
						mes="推荐人不能为自己";
					}
				}
			}			
			
		}
		if(flag) {
			
				Card card = cardMapper.queryCardById(cardid);
				if(card==null){
					result.put("code", 500);
					result.put("message","购买会员卡失败,会员卡信息不存在");			
				}else{
					User oruser = userMapper.queryUserByTel(tel);					
					if(oruser!=null){
						User user = userMapper.queryUserById(oruser.getUserid());
						User upUser = new User();
						upUser.setUserid(user.getUserid());
						upUser.setPoints(user.getPoints()+card.getPoint());
						userMapper.updateUserMes(upUser);				
					}
					User updUser = new User();
					updUser.setUserid(usermes.getUserid());								
					updUser.setCardovertime(DateUtil.AfterMonthDate(card.getLife()));
					updUser.setPoints(usermes.getPoints()+card.getPoint());		
					updUser.setCardname("会员");
					userMapper.updateUserMes(updUser);
					CardRecord cardRecord =  new CardRecord();
					cardRecord.setCardname(card.getCardname());
					cardRecord.setCardrecordid(PrimaryKeyGenerate.getInstance("CRO"));				
					cardRecord.setUserid(usermes.getUserid());
					cardRecord.setPayway("1");
					cardRecord.setVersion(0);
					cardRecordMapper.insertCardRecord(cardRecord);
					result.put("code", 200);
					result.put("message","购买会员卡成功");		
				}

		}else {
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> getCardRecord(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();	
		String tokenid = map.get("tokenid")+"";		
		boolean flag = true;	
		String mes = "";				
		User usermes = new User();
		if(map.get("tokenid")==null||"".equals(tokenid)){
			flag = false;
			mes="token不能为空";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}
			}			
			
		}
		if(flag) {
			List<CardRecord> cardRecords = cardRecordMapper.getCardRecordByUserId(usermes.getUserid());
			result.put("data", cardRecords);
			result.put("code", 200);
			result.put("message", "获取购卡记录信息成功");	
		}else{
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Transactional(propagation=Propagation.REQUIRED,rollbackFor = Exception.class)
	@Override
	public Map<String, Object> orderCourse(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();	
		String tokenid = map.get("tokenid")+"";	
		String courseid = map.get("courseid")+"";	
		boolean flag = true;	
		String mes = "";				
		User usermes = null;
		Course course = null;
		if(map.get("tokenid")==null||"".equals(tokenid)){
			flag = false;
			mes="token不能为空";
		}if(map.get("courseid")==null||"".equals(courseid)){
			flag = false;
			mes="预约课程不能为空";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}else if("非会员".equals(usermes.getCardname())){
					flag = false;
					mes="该用户不是会员";					
				}else if((DateUtil.String2Date(usermes.getCardovertime()).before(new Date()))&&(usermes.getCardnum()==0)){
					flag = false;
					mes="会员卡已过期";	
				}else if(DateUtil.String2Date(DateUtil.AfterDateTime(usermes.getDefoulttime(),3)).after(new Date())){
					flag = false;
					mes="无故旷课三天不可预约";	
				}else{
					course = courseMapper.findCourseById(courseid);
					if(course==null){
						flag = false;
						mes="预约课程失败,课程信息不存在";
					}else if(DateUtil.String2Date(course.getStarttime()).before(new Date())){
						flag = false;
						mes = "预约课程失败,课程已开始或结束";
					}else if(DateUtil.String2Date(course.getOrdertime()).after(new Date())){
						flag = false;
						mes = "预约课程失败,该课程的预约时间未到";
					}else{
						Map<String, Object> param =  new HashMap<String, Object>();	
						param.put("starttime", course.getStarttime());
						param.put("endtime", course.getEndtime());
						Integer timeCount = courseUserMapper.queryCoursesCount(param);
						if(timeCount<=0){
							param =  new HashMap<String, Object>();	
							param.put("courseid", courseid);
							param.put("userid", usermes.getUserid());
							CourseUser cu = courseUserMapper.queryCourseUser(param);
							if(cu==null){
								if(course.getOrdernum()>=course.getTotalnum()){
									flag = false;
									mes = "预约课程失败,预约人数已满";
								}
							}else{
								flag = false;
								mes = "预约课程失败,已预约该课程";
							}
						}else{
							flag = false;
							mes = "预约课程失败,该时间段已预约课程";
						}
						
					}
				}
			}			
			
		}
		if(flag) {
			Integer type = 1;
			if(DateUtil.String2Date(usermes.getCardovertime()).before(new Date())){
				type = 2;
				User userMes = new User();
				userMes.setUserid(usermes.getUserid());
				userMes.setCardnum(usermes.getCardnum()-1);					
				userMapper.updateUserMes(userMes);
			}
			Course upCourse = new Course();
			upCourse.setCourseid(courseid);
			upCourse.setOrdernum(course.getOrdernum()+1);
			courseMapper.updateCourse(upCourse);
			CourseUser courseUser = new CourseUser();
			courseUser.setId(PrimaryKeyGenerate.getInstance("CUR"));	
			courseUser.setUserid(usermes.getUserid());
			courseUser.setCourseid(courseid);
			courseUser.setState("1");	
			courseUser.setVersion(0);
			courseUser.setType(type);
			courseUserMapper.insertCourseUser(courseUser);
			result.put("code", 200);
			result.put("message","预约课程成功");	

		}else{
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Transactional(propagation=Propagation.REQUIRED,rollbackFor = Exception.class)
	@Override
	public Map<String, Object> unOrderCourse(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();	
		String tokenid = map.get("tokenid")+"";	
		String courseid = map.get("courseid")+"";	
		boolean flag = true;	
		String mes = "";				
		User usermes = null;
		CourseUser cu = null;
		Course course = null;
		if(map.get("tokenid")==null||"".equals(tokenid)){
			flag = false;
			mes="token不能为空";
		}if(map.get("courseid")==null||"".equals(courseid)){
			flag = false;
			mes="预约课程不能为空";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}else{
						Map<String, Object> param =  new HashMap<String, Object>();	
						param.put("courseid", courseid);
						param.put("userid", usermes.getUserid());
						cu = courseUserMapper.queryCourseUser(param);
						if(cu==null){
							flag = false;
							mes="取消预约课程失败,找不到预约信息";
						}else{
							String second=constantMapper.queryConstantByName("UNORDERTIME").getValue();
							course = courseMapper.findCourseById(courseid);
							if(DateUtil.AfterSSDate(Integer.parseInt(second)).after(DateUtil.String2Date(course.getStarttime()))){
								flag = false;
								mes="取消预约课程失败,已超出可取消时间";
							}
						}
				}
			}			
			
		}
		if(flag) {
			if(cu.getType()==2){
				User userMes = new User();
				userMes.setUserid(usermes.getUserid());
				userMes.setCardnum(usermes.getCardnum()+1);					
				userMapper.updateUserMes(userMes);
			}
			Course upCourse = new Course();
			upCourse.setCourseid(courseid);
			upCourse.setOrdernum(course.getOrdernum()-1);
			courseMapper.updateCourse(upCourse);
			CourseUser courseUser = new CourseUser();			
			courseUser.setState("0");	
			courseUser.setId(cu.getId());
			courseUserMapper.updateCourseUser(courseUser);
			result.put("code", 200);
			result.put("message","取消预约课程成功");	

		}else{
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> findCourseMes(Map<String, Object> map) throws DataAccessException {		
		Map<String, Object> result =  new HashMap<String, Object>();	
		String tokenid = map.get("tokenid")+"";	
		String courseid = map.get("courseid")+"";	
		boolean flag = true;	
		String mes = "";	
		String userid="";
		if(map.get("courseid")==null||"".equals(courseid)){
			flag = false;
			mes="课程不能为空";
		}else if(map.get("tokenid")!=null&&!"".equals(tokenid)){
			User userMes=userMapper.queryUserByToken(tokenid);
			if(userMes!=null){			
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(userMes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User user = new User();
					user.setUserid(userMes.getUserid());
					user.setTokenid(tokenid);					
					user.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(user);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}else{
					userid=userMes.getUserid();		
				}			
				
			}		
		}
		
		if(flag){
			map.put("userid", userid);			
			Course course = courseMapper.getCourse(map);
			Constant constant = constantMapper.queryConstantByName("IMAGE_URLHEAD");	
			course.setCoachIcon(constant.getValue()+course.getCoachIcon());
			course.setPicture(constant.getValue()+course.getPicture());
			course.setStorePicture(constant.getValue()+course.getStorePicture());		
			result.put("data", course);
			result.put("code", 200);
			result.put("message", "获取课程信息成功");			
		}else{
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> homeHead(Map<String, Object> map) throws DataAccessException {		
		Map<String, Object> result =  new HashMap<String, Object>();
		String url= constantMapper.queryConstantByName("HOMEHEAD").getValue();
		result.put("code", 200);
		result.put("message","获取头图片路径成功");
		result.put("data", url);
		return result;
	}

	@Override
	public Map<String, Object> getCourseRecord(Map<String, Object> map)	throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();	
		String tokenid = map.get("tokenid")+"";		
		boolean flag = true;	
		String mes = "";	
		String userid="";
		if(map.get("tokenid")==null||"".equals(tokenid)){
			flag = false;
			mes="token不能为空";
		}else {
			User userMes=userMapper.queryUserByToken(tokenid);
			if(userMes!=null){			
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(userMes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User user = new User();
					user.setUserid(userMes.getUserid());
					user.setTokenid(tokenid);					
					user.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(user);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}else{
					userid=userMes.getUserid();		
				}			
				
			}		
		}
		
		if(flag){
			map.put("userid", userid);			
			List<Course> courses = courseMapper.getCourseRecord(map);
			Map<String, Object> data =  new HashMap<String, Object>();		
			data.put("list", courses);
			Constant constant = constantMapper.queryConstantByName("IMAGE_URLHEAD");	
			data.put("imgUrl", constant.getValue());
			result.put("data", data);
			result.put("code", 200);
			result.put("message", "获取课程信息成功");			
		}else{
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> getVersion(Map<String, Object> map)	throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();	
		String version = map.get("version")+"";		
		boolean flag = true;	
		String mes = "";		
		if(map.get("version")==null||"".equals(version)){
			flag = false;
			mes="版本号不能为空";
		}		
		if(flag){
			//最新VERSION
			String vs= constantMapper.queryConstantByName("VERSION").getValue();
			//最新版本的URL
			String url= constantMapper.queryConstantByName("NEWURL").getValue();		
			//已经是最新了
			if(!vs.equals(version)){			
				Map<String, Object> data =  new HashMap<String, Object>();	
				data.put("version_name", "版本名称");
				data.put("version_code", vs);
				data.put("version_url", url);
				data.put("version_msg", "版本信息");
				result.put("data", data);
				result.put("code", 200);
				result.put("message","获取版本信息成功");
			}else{
				result.put("code", 210);
				result.put("message","已经为最新版本");
			}				
		}else{
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Override
	public Map<String, Object> getOrderedMember(Map<String, Object> map) throws DataAccessException {
		Map<String, Object> result =  new HashMap<String, Object>();	
		String courseid = map.get("courseid")+"";		
		boolean flag = true;	
		String mes = "";		
		if(map.get("courseid")==null||"".equals(courseid)){
			flag = false;
			mes="课程不能为空";
		}		
		if(flag){			
			List<User> users = userMapper.getOrderedMember(map);
			Map<String, Object> data =  new HashMap<String, Object>();		
			data.put("list", users);
			data.put("imgUrl", constantMapper.queryConstantByName("URL_FRONT").getValue()+constantMapper.queryConstantByName("ICONURL").getValue());
			result.put("data", data);
			result.put("code", 200);
			result.put("message", "获取预约用户成功");		
		}else{
			throw new DataAccessException(mes);		
		}
		return result;
	}
	
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor = Exception.class)
	@Override
	public Map<String, Object> alipay(Map<String, Object> map)	throws Exception {
		Map<String, Object> result =  new HashMap<String, Object>();	
		String tel = map.get("tel")+"";
		String tokenid = map.get("tokenid")+"";	
		String cardid = map.get("cardid")+"";	
		boolean flag = true;	
		String mes = "";				
		User usermes = new User();
		if(map.get("tokenid")==null||"".equals(tokenid)){
			flag = false;
			mes="token不能为空";
		}else if(map.get("cardid")==null||"".equals(cardid)){
			flag = false;
			mes="会员卡不能为空";
		}else{
			usermes =userMapper.queryUserByToken(tokenid);
			if(usermes==null){
				flag = false;
				mes="token不正确，验证失败";
			}else{
				//token还没设置，或则token过期，重新设置				
				if(DateUtil.String2Date(usermes.getTokenovertime()).before(new Date())){
					tokenid=PrimaryKeyGenerate.getInstance("TOK");
					User userMes = new User();
					userMes.setUserid(usermes.getUserid());
					userMes.setTokenid(tokenid);					
					userMes.setTokenovertime(DateUtil.getAfterSecond(Integer.parseInt(constantMapper.queryConstantByName("TOKENLIFE").getValue())));
					userMapper.updateUserMes(userMes);
					result.put("code", 999);
					result.put("message", "token过期");
					return result;
				}else{
					User myuser = userMapper.queryUserById(usermes.getUserid());
					if(map.get("tel")!=null&&tel.equals(myuser.getTel())){
						flag = false;
						mes="推荐人不能为自己";
					}
				}
			}			
			
		}
		if(flag) {
			
				Card card = cardMapper.queryCardById(cardid);
				if(card==null){
					result.put("code", 500);
					result.put("message","购买会员卡失败,会员卡信息不存在");			
				}else{
					/*User oruser = userMapper.queryUserByTel(tel);					
					if(oruser!=null){
						User user = userMapper.queryUserById(oruser.getUserid());
						User upUser = new User();
						upUser.setUserid(user.getUserid());
						upUser.setPoints(user.getPoints()+card.getPoint());
						userMapper.updateUserMes(upUser);				
					}
					User updUser = new User();
					updUser.setUserid(usermes.getUserid());								
					updUser.setCardovertime(DateUtil.AfterMonthDate(card.getLife()));
					updUser.setPoints(usermes.getPoints()+card.getPoint());		
					updUser.setCardname("会员");
					userMapper.updateUserMes(updUser);*/
					CardRecord cardRecord =  new CardRecord();
					cardRecord.setCardname(card.getCardname());
					cardRecord.setPoint(card.getPoint());
					cardRecord.setCardrecordid(PrimaryKeyGenerate.getInstance("CRO"));				
					cardRecord.setUserid(usermes.getUserid());
					cardRecord.setPayway("1");
					cardRecord.setVersion(0);
					cardRecord.setTel(tel);
					cardRecord.setState(0);
					cardRecord.setLife(card.getLife());
					cardRecord.setType(card.getType());
					cardRecord.setPrice(card.getCardprice());
					cardRecordMapper.insertCardRecord(cardRecord);
				
					/*Map<String, String> mapp = new HashMap<String, String>();
					mapp.put("app_id", AlipayConfig.app_id);
					mapp.put("method", "alipay.trade.app.pay");
					mapp.put("format", "json");
					mapp.put("charset", "utf-8");
					mapp.put("sign_type", "RSA");
					mapp.put("timestamp", DateUtil.getNowDate());
					mapp.put("version", "1.0");
					mapp.put("notify_url", AlipayConfig.service);

			        Map<String, String> m = new HashMap<String, String>();

			        m.put("body", "金舞团会员卡购买");
			        m.put("subject", "金舞团会员卡");
			        m.put("body", "jwthykgm");
			        m.put("subject", "jwthyk");
			        m.put("out_trade_no", cardRecord.getCardrecordid());
			        m.put("timeout_express", "30m");
			        m.put("total_amount", card.getCardprice()+"");
			        m.put("seller_id", AlipayConfig.partner);
			        m.put("product_code", "QUICK_MSECURITY_PAY");

			        JSONObject bizcontentJson= JSONObject.fromObject(m);
			        mapp.put("biz_content", URLEncoder.encode(bizcontentJson.toString(), "UTF-8"));			        
			        //对未签名原始字符串进行签名       
			        String rsaSign = AlipaySignature.rsaSign(mapp, AlipayConfig.private_key, "utf-8");			        
			        Map<String, String> par = AlipayCore.paraFilter(mapp); //除去数组中的空值和签名参数
			        String json4 = AlipayCore.createLinkString(par);   //拼接后的字符串
			        json4=json4 + "&sign=" + URLEncoder.encode(rsaSign, "UTF-8");
			        result.put("data", json4.toString());		*/	
					
				    Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(AlipayConfig.app_id, true,card.getCardprice(),cardRecord.getCardrecordid());
			        String orderParam = OrderInfoUtil2_0.buildOrderParam(params);		
			        String sign = OrderInfoUtil2_0.getSign(params, AlipayConfig.private_key, true);
			        String orderInfo2 = orderParam + "&" + sign;
			        result.put("data", orderInfo2);
					result.put("code", 200);
					result.put("message","购买会员卡成功");		
				}

		}else {
			throw new DataAccessException(mes);		
		}
		return result;
	}

	@Transactional(propagation=Propagation.REQUIRED,rollbackFor = Exception.class)
	@Override
	public void updateCardStatus(String cardrecordid) throws DataAccessException {
		cardRecordMapper.updateCardStatus(cardrecordid);
		CardRecord cardRecord = cardRecordMapper.getCardRecordById(cardrecordid);
		if(cardRecord.getTel()!=null&&!"".equals(cardRecord.getTel())){
			User oruser = userMapper.queryUserByTel(cardRecord.getTel());					
			if(oruser!=null){			
				User upUser = new User();
				upUser.setUserid(oruser.getUserid());
				upUser.setPoints(oruser.getPoints()+cardRecord.getPoint());
				userMapper.updateUserMes(upUser);			
			}
			User usermes = userMapper.queryUserById(cardRecord.getUserid());
			User updUser = new User();
			updUser.setUserid(usermes.getUserid());
			if(cardRecord.getType()==1){
				updUser.setCardovertime(DateUtil.AfterMonthDate(cardRecord.getLife()));
			}else{
				updUser.setCardnum(usermes.getCardnum()+cardRecord.getLife());
			}
			updUser.setPoints(usermes.getPoints()+cardRecord.getPoint());		
			updUser.setCardname("会员");
			userMapper.updateUserMes(updUser);			
		}
	}

}
